using System.Collections.Generic;
using System.Globalization;
using P2.Framework.BOLBase;
using P2.Framework.Helper;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;

using System.ComponentModel;
using System;
using Microsoft.Practices.Composite.Wpf.Commands;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Regions;
using System.Windows;
namespace P2.SimpleAccounting.Modules.Users
{
    public class UsersItemDetailPresentationModel : INotifyPropertyChanged, IDataErrorInfo
    {
        public UsersItemDetailPresentationModel(IUsersItemDetailsView view, BusinessEntities.UsersItem model, Services.IUsersService service
                    , IUnityContainer container, IRegionManager regionManager
            )
        {
            View = view;
            this.container = container;
            this.regionManager = regionManager;
            this.Model = model;
            this.Service = service;

                        

            Validator = new P2Validator(Properties.Settings.Default.ModuleName, "UsersItemDetailView", "UsersItem", "UsersItemID");
            
                Validator.PropertyValidators.Add(new P2ValidationItemDecimal()
                {
                    ClassName = "UsersItem",
                    Mandatory = true,
                    ValidationType = P2ValidationStatus.red,
                    MinValue = decimal.MinValue,
                    MaxValue = decimal.MaxValue,
                    PropertyName = "UsersItemAmount",
                    NonZero = true

                });
            
                Validator.PropertyValidators.Add(new P2ValidationItemDecimal()
                {
                    ClassName = "UsersItem",
                    Mandatory = true,
                    ValidationType = P2ValidationStatus.red,
                    MinValue = decimal.MinValue,
                    MaxValue = decimal.MaxValue,
                    PropertyName = "UsersItemQuantity",
                    NonZero = true
                });
            

            ControlRights = Model.GetControls(Properties.Settings.Default.ModuleName, "UsersItemDetailView");
            this.UsersItemSaveCommand = new DelegateCommand<object>(OnUsersItemSaveExecute, OnUsersItemSaveCanExecute);
            this.UsersItemCloseCommand = new DelegateCommand<object>(OnUsersItemCloseExecute, OnUsersItemCloseCanExecute);
            
            View.Model = this;
            RefreshView();
        }

        private IUnityContainer container;
        private IRegionManager regionManager;
        public IUsersItemDetailsView View { get; set; }

        public BusinessEntities.UsersItem Model { get; set; }

        public List<P2.SimpleAccounting.Modules.FiAccount.BusinessEntities.FiAccountModel> AvailableAccounts;
        public Services.IUsersService Service;
        public P2Validator Validator { get; set; }
        public Dictionary<string, P2.Framework.Security.User.ControlRight> ControlRights;
        protected P2MessageStack _myMessageStack = new P2MessageStack();
        public P2MessageStack MessageStack
        {
            get
            {
                return _myMessageStack;
            }
            set
            {
                _myMessageStack = value;
                OnPropertyChanged("MessageStack");
            }
        }


        #region "Members"
        public int UsersItemID
        {
            get { return Model.UsersItemID; }
        }
        
               

        public decimal UsersItemQuantity
        {
            get { return Model.UsersItemQuantity; }
            set
            {
                Model.UsersItemQuantity = value;
                OnPropertyChanged("UsersItemQuantity");
            }
        }

        public decimal UsersItemAmount
        {
            get { return Model.UsersItemAmount; }
            set
            {
                Model.UsersItemAmount = value;
                OnPropertyChanged("UsersItemAmount");
            }
        }



        public bool ModelIsModified
        {
            get
            {
                return Model.Modified;
            }
        }
        public BusinessEntities.Users ParentUsers
        {
            get
            {
                return Model.ParentUsers;
            }
        }


        #endregion

        #region "Commands"
        public DelegateCommand<object> UsersItemSaveCommand { get; set; }
        public DelegateCommand<object> UsersItemCloseCommand { get; set; }
        public DelegateCommand<object> UsersItemFindCommand { get; set; }
        void OnUsersItemSaveExecute(object sourceAccount)
        {
            View.UpdateFocusedControl();
            if (this.Save())
            {
                Model.OnAllPropertiesChanged();
                View.Close(true);
            }
            else
                this.RefreshView();
            return;
        }

        bool OnUsersItemSaveCanExecute(object sourceAccount)
        {
            return true;
        }

        void OnUsersItemCloseExecute(object sourceAccount)
        {
            View.UpdateFocusedControl();
            {
                View.Close(false);

            }
        }

        bool OnUsersItemCloseCanExecute(object sourceAccount)
        {
            return true;
        }

        

        bool OnUsersItemFindCanExecute(object sourceAccount)
        {
            return true;
        }


        public void RefreshView()
        {
            OnPropertyChanged("MessageStack");
            OnPropertyChanged("UsersItemID");
            
            OnPropertyChanged("UsersItemQuantity");
            OnPropertyChanged("UsersItemAmount");
        }


        #endregion
        public bool Save()
        {
            MessageStack.Clear();
            MessageStack.AddRange(Validator.Validate(this.Model));
            if (MessageStack.StatusMessage.MessageStatus == P2ValidationStatus.red)
            {
                return false;
            }

            return true;

        }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }
        /*
        private void OnModelPropertyChangedEvent(object sender, PropertyChangedEventArgs e)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(sender, e);

        }
        */



        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get
            {

                string result = null;

             
                P2MessageStack propertyMessageStack = this.Validator.ValidateProperty(this.Model, columnName);
                if (propertyMessageStack != null && propertyMessageStack.Count > 0)
                {
                    foreach (P2TransactionMessage message in propertyMessageStack)
                    {
                        if (message.TransactionStatus != "green")
                        {
                            if (result == null)
                                result = message.MessageText;
                            else
                                result += "\n" + message.MessageText;
                        }
                    }
                }

                return result;
            }
        }

        #endregion
    }
}